import UserModel from "../../schema/user";
import { Request, Response } from "express";
import type Typings from "../../typings";

// Common 类的实例
import { Common, ResStatus } from "../common/common";
const CommonIns = new Common(UserModel);
const ResStatusIns = new ResStatus();

class User {
  constructor() {}

  // 查看用户列表
  async list(req: Request, res: Response) {
    try {
      // const creatorId = CommonIns.getLoginUserId(
      //   req as Typings.Express.ConvertRequest
      // );
      let { page = 1, size = 10, name = "" } = req.query;
      page = Number(page);
      size = Number(size);

      // const query: { creatorId: string; name?: any } = {
      //   creatorId,
      // };

      const query: { name?: any } = {};

      if (name) {
        query.name = { $regex: name, $options: "i" };
      }

      const total = await CommonIns.Count(query);
      const users: Typings.User.instance[] = await CommonIns.Find(query)
        .skip(size * (page - 1))
        .limit(size)
        .sort({
          createdTime: -1,
        });

      const data = {
        page,
        size,
        list: users,
        total,
      };

      ResStatusIns.Success(res, data);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 查看单个用户
  async check(req: Request, res: Response) {
    try {
      const userId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );
      let { id } = req.query;

      if (!id && !userId) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 查询用户数据
      const users: Typings.User.instance[] = await CommonIns.Find({
        id: id || userId,
      });

      if (!users.length) {
        // 用户不存在
        ResStatusIns.isNotExist(res);
        return;
      }

      // 用户存在时
      ResStatusIns.Success(res, users[0]);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 新增用户
  async add(req: Request, res: Response) {
    try {
      const creatorId = CommonIns.getLoginUserId(
        req as Typings.Express.ConvertRequest
      );

      const { account, password, name, gender, roleId, avatar, desc } =
        req.body;

      console.log("-=======================", req.body);

      const users: Typings.User.instance[] = await CommonIns.Find({
        account,
      });

      if (users.length) {
        ResStatusIns.isExist(res);
        return;
      }

      await CommonIns.SaveOne({
        account,
        password,
        name,
        gender,
        roleId,
        avatar,
        desc,
        creatorId: creatorId || req.body.creatorId,
        createdTime: Date.now(),
        updatedTime: Date.now(),
      });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 更新用户信息
  async update(req: Request, res: Response) {
    try {
      const { id, password, name, gender, roleId, avatar, desc } = <
        Partial<Typings.User.instance>
      >req.body;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      const updateContent = {
        password,
        name,
        gender,
        roleId,
        avatar,
        desc,
        updatedTime: Date.now(),
      };

      await CommonIns.UpdateOne({ id }, updateContent);
      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }

  // 删除单个用户
  async remove(req: Request, res: Response) {
    try {
      let { id } = req.query;

      if (!id) {
        ResStatusIns.paramsWrong(res);
        return;
      }

      // 删除用户数据
      await CommonIns.RemoveOne({ id });

      ResStatusIns.Success(res);
    } catch (err) {
      ResStatusIns.Database(res, err);
    }
  }
}

export default User;
